﻿#if NETFX || NETCORE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Apewer.Surface
{

    /// <summary>基本窗体。</summary>
    public class BaseForm : Form
    {

        #region this & base

        private IContainer _components = null;

        private void InitializeComponent()
        {
            this.SuspendLayout();

            this.AutoScaleMode = AutoScaleMode.None;
            this.BackColor = FormsUtility.White;
            this.Font = FormsUtility.DefaultFont;
            this.ClientSize = new Size(600, 400);
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.KeyPreview = true;

            this.ResumeLayout(false);
        }

        /// <summary>构造函数。</summary>
        public BaseForm()
        {
            InitializeComponent();
        }

        #endregion

        #region override

        private bool _resizeable = true;

        /// <summary>是否能够调整窗体大小。</summary>
        public bool Resizeable
        {
            get { return _resizeable; }
            set { _resizeable = value; }
        }

        /// <summary>释放资源。</summary>
        /// <param name="disposing">释放非托对象。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (_components != null)) _components.Dispose();
            base.Dispose(disposing);
        }

        ///// <summary>允许点击任务栏最小化。</summary>
        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        const int WS_MINIMIZEBOX = 0x00020000;  // winuser.h
        //        CreateParams cp = base.CreateParams;
        //        cp.Style = cp.Style | WS_MINIMIZEBOX;   // 允许最小化操作
        //        return cp;
        //    }
        //}

        /// <summary>允许调节窗口大小。</summary>  
        protected override void WndProc(ref Message m)
        {
            const int htleft = 10;
            const int htright = 11;
            const int httop = 12;
            const int httopleft = 13;
            const int httopright = 14;
            const int htbottom = 15;
            const int htbottomleft = 0x10;
            const int htbottomright = 17;

            switch (m.Msg)
            {
                case 0x0084:
                    base.WndProc(ref m);
                    if (Resizeable)
                    {
                        Point vPoint = new Point((int)m.LParam & 0xFFFF,
                            (int)m.LParam >> 16 & 0xFFFF);
                        vPoint = PointToClient(vPoint);
                        if (vPoint.X <= 5)
                            if (vPoint.Y <= 5)
                                m.Result = (IntPtr)httopleft;
                            else if (vPoint.Y >= ClientSize.Height - 5)
                                m.Result = (IntPtr)htbottomleft;
                            else m.Result = (IntPtr)htleft;
                        else if (vPoint.X >= ClientSize.Width - 5)
                            if (vPoint.Y <= 5)
                                m.Result = (IntPtr)httopright;
                            else if (vPoint.Y >= ClientSize.Height - 5)
                                m.Result = (IntPtr)htbottomright;
                            else m.Result = (IntPtr)htright;
                        else if (vPoint.Y <= 5)
                            m.Result = (IntPtr)httop;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)htbottom;
                    }
                    break;
                case 0x0201: // 左键
                    m.Msg = 0x00A1; // 更改消息为非客户区按下鼠标 
                    m.LParam = IntPtr.Zero; // 默认值 
                    m.WParam = new IntPtr(2); // 鼠标放在标题栏内 
                    base.WndProc(ref m);
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        #endregion

        #region public accessor 

        #endregion

        #region protected derivation

        /// <summary>界面线程调用器。</summary>
        protected delegate void Invoker();

        /// <summary>优化性能：开启双缓冲。</summary>
        protected void Optimize()
        {
            // SetStyle(ControlStyles.ResizeRedraw, value);
            // SetStyle(ControlStyles.Selectable, true);

            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.DoubleBuffer, true);

            // SetStyle(ControlStyles.Opaque, true);

            UpdateStyles();
        }

        #endregion

    }

}

#endif
